﻿
dynamic class D2.Dock extends MovieClip
{
	private var icons:Array;
	private var scops:Array;
	private var toFlowId:Number = 0;
	private var mx:Number;
	private var my:Number;
	
	public var MinScale:Number=100;
	public var MaxScale:Number = 230;
	public var OffsetW:Number = 10;
	public var Enable:Boolean = true;
	public var EFScope:Number = 100;
	/**
	 * left/icon/right
	 */
	private var FixType:String = "icon";
	private var FixIconWidth:Boolean = false;
	public function Dock() {
	}
	
	public function initIcons(icons:Array) {
		if (icons != undefined) {
			this.icons = icons;
			this.onEnterFrame = this.RunFun;
			init();
		}else {
			trace("Dock cant init undefined icon array.");
		}
	}
	private function RunFun() {
		
		flowIcon();
		if (!Enable) {
			setIconScale();
			return;
		}else{
			mx= this._parent._xmouse;
			my = this._parent._ymouse;
		}
		
		var id:Number = getMouseOverItem();
		
		if(id>-1){
			this.toFlowId = id;
			setIconScale();
		}else {
			setIconScale(false);
		}
		
		var scope:Object = scops[this.toFlowId];
		var icon:MovieClip = icons[this.toFlowId];
		var cx:Number = (scope.right + scope.left)/2;
		if (id >= 0) {
			var w = icon._width+OffsetW - (scope.right - scope.left);
			//w/(scope.right - scope.left)
			icon._x = cx  + (cx - mx) * (w / (scope.right - scope.left));
		}else {
			icon._x = cx;
		}
	}
	public function UpdataNowFlowId(icon:MovieClip) {
		
		mx= this._parent._xmouse;
		my = this._parent._ymouse;
		
		var id:Number = this.toFlowId;
		if(icon==undefined){
			id=getMouseOverItem();
		}else {
			for (var i = 0; i < icons.length; i++) {
				if (icon == icons[i]) {
					id = i;
				}
			}
		}
		if(id>-1){
			this.toFlowId = id;
		}
		trace("now id::::"+this.toFlowId);
	}
	private function setIconScale(f:Boolean) {
		if (f == undefined) {
			f = true;
		}
		var xscalev:Number =0;
		var yscalev:Number =0;
		for (var i = 0; i < icons.length; i++) {
			if (!Enable && i == toFlowId) {
				continue;
			}
			var s = EFScope - Math.abs(mx-(scops[i].left+scops[i].right)/2);
			if (s > 0 && f) {
				xscalev = ((MaxScale-MinScale) * s / EFScope + MinScale)- icons[i]._xscale;
				yscalev = ((MaxScale-MinScale) * s / EFScope + MinScale)- icons[i]._yscale;
				icons[i]._xscale += (xscalev) / 3;
				icons[i]._yscale += (yscalev) / 3;
			}else {
				icons[i]._xscale += (MinScale- icons[i]._xscale) / 3;
				icons[i]._yscale += (MinScale- icons[i]._yscale) / 3;
			}
		}
	}
	private function flowIcon() {
		var X:Number = 0;
		for (var i:Number = this.toFlowId - 1; i >= 0; i--) {
			X = icons[i + 1]._x - icons[i + 1]._width / 2 - OffsetW - icons[i]._width / 2
			icons[i]._x += (X-icons[i]._x)/3;
		}
		for (var i:Number = this.toFlowId + 1; i < icons.length; i++) {
			X =icons[i - 1]._x + icons[i - 1]._width / 2 + OffsetW + icons[i]._width / 2;
			icons[i]._x += (X-icons[i]._x)/3;
		}
	}
	private function getMouseOverItem():Number{
		
		for (var i = 0; i < scops.length; i++) {
			if (mx >= scops[i].left && mx <= scops[i].right) {
				if(Math.abs(icons[i]._y-my)< EFScope){
					return i;
				}
			}
		}
		return -1;
	}
	private function init() {
		scops = new Array();
		var w:Number = OffsetW;
		trace(icons.length);
		for (var i = 0; i < icons.length; i++) {
			MovieClip(icons[i])._xscale = MovieClip(icons[i])._yscale = MinScale;
			w += icons[i]._width+ OffsetW;
			
		}
		for (var i = 0; i < icons.length; i++) {
			if (i == 0) {
				icons[0]._x = this._x -w / 2 + icons[0]._width / 2 +OffsetW;
			}
			else {
				icons[i]._x = icons[i - 1]._x + icons[i - 1]._width/2 + icons[i]._width / 2 + OffsetW;
			}
			icons[i]._y = this._y;
			scops.push({ 
				left:icons[i]._x - (icons[i]._width + OffsetW) / 2, 
				right:icons[i]._x + (icons[i]._width + OffsetW) / 2,
				toString:function(){return "l:"+this.left+" r:"+this.right+"\r"}
				});
		}
		trace(scops);
	}
}